package java8;

import java8.entity.Employee;

import java.util.Arrays;
import java.util.List;
import java.util.Optional;

public class MatchFind {
    public static void main(String[] args) {

        Employee e1 = new Employee(1, 22, "F", "aa", "aa");
        Employee e2 = new Employee(2, 23, "M", "bb", "bb");
        Employee e3 = new Employee(3, 24, "F", "cc", "cc");
        Employee e4 = new Employee(4, 25, "M", "dd", "dd");
        Employee e5 = new Employee(5, 26, "M", "ee", "ee");
        Employee e6 = new Employee(6, 27, "F", "gg", "gg");

        List<Employee> employees = Arrays.asList(e1, e2, e3, e4, e5, e6);
        //是否存在年龄 > 24 的人，返回的类型是 boolean
        //这里的箭头函数可以用谓词进行存储，这样好方便进行复用
        boolean age24 = employees.stream().anyMatch(Employee.thanAge);
        System.out.println("age24 = " + age24);
        //判断是否所有的元素都 > 23
        boolean age23 = employees.stream().allMatch(e -> e.getAge() > 23);
        System.out.println("age23 = " + age23);
        //判断是否所有的元素都不 > 25
        boolean age25 = employees.stream().noneMatch(e -> e.getAge() >25);
        System.out.println("age25 = " + age25);

        //查找第一个年龄 > 26 的员工 这里还可以写 findAny() 作用是查找任意一个
        Optional<Employee> optionalEmployee
                = employees.stream().filter(e -> e.getAge() > 26).findFirst();
        //注意这里拿到的是整个对象 (如果不存在的话这里的 get 方法就会抛出异常)
        System.out.println("optionalEmployee = " + optionalEmployee.get());

        //判断这样一个类型是否存在
        boolean isPresent = employees.stream().filter(e -> e.getAge() > 26).findFirst().isPresent();
        System.out.println("isPresent = " + isPresent);

        //如果没有找到的话就给出一个默认值
        Employee employee = employees.stream().filter(e -> e.getAge() > 26).findFirst().orElse(new Employee());
        System.out.println("employee = " + employee);

    }
}
